package com.yt.common.utils;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import org.apache.commons.beanutils.BeanUtils;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class publicUtil {
	public publicUtil() {
	}
	public static Map<String, String> json2map(String str_json) {
		Map<String, String> res = null;
		try {
			Gson gson = new Gson();
			res = gson.fromJson(str_json, new TypeToken<Map<String, String>>() {
			}.getType());
		} catch (JsonSyntaxException e) {
		}
		return res;
	}
	public static HashMap<String,String> returnMap(String code,String url,String message){

		HashMap<String, String> returnMap = new HashMap<String, String>();
		returnMap.put("CODE", code);
		returnMap.put("MESSGE", message);
		returnMap.put("QR_CODE_DATA", url);
		return returnMap;
	}
	public static HashMap<String,Object> returnMapCommon(String code,String message,Object detail){

		HashMap<String, Object> returnMap = new HashMap<String, Object>();
		returnMap.put("CODE", code);
		returnMap.put("MESSGE", message);
		returnMap.put("DETAIL", detail);
		return returnMap;
	}
	public static <T> T convertMap2Bean(Map<String, Object> map, Class<T> T)
			throws Exception
	{
		if (map == null || map.size() == 0)
		{
			return null;
		}
		//获取map中所有的key值，全部更新成大写，添加到keys集合中,与mybatis中驼峰命名匹配
		Object mvalue;
		Map<String, Object> newMap = new HashMap<>();
		Iterator<Entry<String, Object>> it = map.entrySet().iterator();
		while(it.hasNext()){
			String key = it.next().getKey();
			mvalue = map.get(key);
          /*  if (key.indexOf(CharacterConstant.UNDERLINE) != -1)
            {
                key = key.replaceAll(CharacterConstant.UNDERLINE, "");
            }*/
			newMap.put(key.toUpperCase(Locale.US), mvalue);
		}

		BeanInfo beanInfo = Introspector.getBeanInfo(T);
		T bean = T.newInstance();
		PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
		for (int i = 0, n = propertyDescriptors.length; i < n; i++)
		{
			PropertyDescriptor descriptor = propertyDescriptors[i];
			String propertyName = descriptor.getName();
			String upperPropertyName = propertyName.toUpperCase();

			if (newMap.keySet().contains(upperPropertyName))
			{
				Object value = newMap.get(upperPropertyName);
				//这个方法不会报参数类型不匹配的错误。
				BeanUtils.copyProperty(bean, propertyName, value);
			}
		}
		return bean;
	}
	public static Map<?, ?> objectToMap(Object obj) {
		if(obj == null)
			return null;

		return new org.apache.commons.beanutils.BeanMap(obj);
	}

	public static String mapToString(HashMap<String, Object> map)
	{
		return JSON.toJSONString(map);
	}

	/**
	 * 获取指定url中的某个参数
	 * @param url
	 * @param name
	 * @return
	 */
	public static String getParamByUrl(String url, String name) {
		url += "&";
		String pattern = "(\\?|&){1}#{0,1}" + name + "=[a-zA-Z0-9]*(&{1})";

		Pattern r = Pattern.compile(pattern);

		Matcher m = r.matcher(url);
		if (m.find( )) {
			System.out.println(m.group(0));
			return m.group(0).split("=")[1].replace("&", "");
		} else {
			return null;
		}
	}

}
